﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;


namespace Ana_Form
{
    public partial class Form_MalzemeCinsi : Form
    {
        DepodataDataContext db;
        MalzemeCinsi Cins;

        public MalzemeCinsi Cinsi
        {
            get { return Cins; }
            set { Cins = value; }
        }
        public Form_MalzemeCinsi(ref DepodataDataContext Db)
        {
            InitializeComponent();
            db = Db;
            Yenile();
        }

        private void Yenile()
        {
            list.Items.Clear();
            var soru = from x in db.MalzemeCinsis
                       orderby x.Adi
                       select x;
            foreach (var item in soru)
            {
                ListViewItem litem = new ListViewItem();
                litem.Text = item.id.ToString();
                litem.SubItems.Add(item.Adi);
                list.Items.Add(litem);
            }
        }
        private void Yenile(IOrderedQueryable<MalzemeCinsi> soru)
        {
            list.Items.Clear();
            foreach (var item in soru)
            {
                ListViewItem litem = new ListViewItem();
                litem.Text = item.id.ToString();
                litem.SubItems.Add(item.Adi);
                list.Items.Add(litem);
            }
        }

        private void btnekle_Click(object sender, EventArgs e)
        {
           string cins= InputBox.Input.Yaz("Malzeme Cinsini Yazınız.");
           if (cins!="")
           {
               Cinsi = new MalzemeCinsi();
               Cinsi.Adi = cins;
               db.MalzemeCinsis.InsertOnSubmit(Cinsi);
               db.SubmitChanges();
           }
           Yenile();
        }

        private void btniptal_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void list_SelectedIndexChanged(object sender, EventArgs e)
        {
            try
            {
                btnTamam.Enabled = true;
                btnduzenle.Enabled = true;
                btnsil.Enabled = true;
                Cinsi = (from x in db.MalzemeCinsis where x.id == int.Parse(list.SelectedItems[0].Text) select x).FirstOrDefault();
            }
            catch (Exception)
            {
            }
        }

        private void btnduzenle_Click(object sender, EventArgs e)
        {
           string cins= InputBox.Input.Yaz("Malzeme Cinsini Düzenleyiniz.", list.SelectedItems[0].SubItems[1].Text);
           Cinsi.Adi = cins;
           db.SubmitChanges();
           Yenile();
        }

        private void btnsil_Click(object sender, EventArgs e)
        {
            if (Cinsi.Malzemes.Count<1)
            {
                db.MalzemeCinsis.DeleteOnSubmit(Cinsi);
                db.SubmitChanges();
                Yenile();
            }
            else
            {
                MessageBox.Show("Bağlantılı Kayıtlar Nedeniyle\nBu Kayıt Silinemez!");
            }
        }

        private void btnTamam_Click(object sender, EventArgs e)
        {
            this.DialogResult = DialogResult.OK;
            this.Close();
        }

        private void list_DoubleClick(object sender, EventArgs e)
        {
            btnTamam_Click(null, null);
        }

        private void list_KeyDown(object sender, KeyEventArgs e)
        {

        }

        private void Form_MalzemeCinsi_KeyDown(object sender, KeyEventArgs e)
        {

        }

        private void txtarama_TextChanged(object sender, EventArgs e)
        {
            var soru = from x in db.MalzemeCinsis
                       where x.Adi.ToUpper().Contains(txtarama.Text.ToUpper())
                       orderby x.Adi
                       select x;
            Yenile(soru);

        }

    }
}
